import { QueryClient } from '@tanstack/vue-query'

export const queryClient = new QueryClient({
  defaultOptions: {}
})

/**
 * @desc 传入一维数组 失效匹配的查询键
 * @desc 传入二维数组 失效一组匹配的查询键
 */
export async function invalidateQueries(queryKey: string[] | Readonly<string[]>): Promise<void>
export async function invalidateQueries(
  queryKey: string[][] | Readonly<string[]>[] | string[] | Readonly<string[]>
): Promise<void | PromiseSettledResult<void>[]> {
  if (typeof queryKey[0] === 'string') {
    return queryClient.invalidateQueries({ queryKey })
  } else {
    return Promise.allSettled(
      (queryKey as string[][]).map((key) => queryClient.invalidateQueries({ queryKey: key }))
    )
  }
}

export type Get = 'get'
export type Put = 'put'
export type Post = 'post'
export type Patch = 'patch'
export type Delete = 'delete'
